package com.edu.whty.entity.system;

import java.util.Arrays;
import java.util.Date;

/**
 * 系统配置表
 * 
 * @author shijiapeng
 * @date 2015年8月21日 下午3:52:16
 */
public class SysConfigPo {
	
	/** 域名+端口 */
	public static final int ADDRESS_TYPE_DOMAIN = 1;
	/** 内网IP+端口 */
	public static final int ADDRESS_TYPE_INNER_IP = 2;
	/** 外网IP+端口 */
	public static final int ADDRESS_TYPE_OUTER_IP = 3;
	
	private String id;// varchar2(32) n 主键
	private String domainId;// varchar2(50) y 平台域名表id
	private String keyName;// varchar2(50) y 键
	private String value;// varchar2(200) y 值
	private String configType;// varchar2(50) y 数据类型，如local-本地配置，aamif-aamif接口配置
	private Integer addressType;// number(1) y 1.域名 2-内网ip地址 3-外网ip地址
	private String describe;// varchar2(200) y 描述
	private Date updateTime = new Date();// date y 修改时间
	private Date createTime = new Date();// date y 创建时间
	private String createrId;// varchar2(50) y 创建者id
	private String createrName;// varchar2(50) y 创建者姓名
	private String platformCode;// varchar2(10) y 平台编码
	private Integer deleted = 0;// number(1) y 是否删除1是0否
	
	//扩展
	private String domain;//域名
	private String addressInfo;//服务器地址如：127.0.0.1:8080
	private String customConfigType;//自定义配置类型

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDomainId() {
		return domainId;
	}

	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}

	public String getKeyName() {
		return keyName;
	}

	public void setKeyName(String keyName) {
		this.keyName = keyName;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getConfigType() {
		return configType;
	}

	public void setConfigType(String configType) {
		this.configType = configType;
	}

	public Integer getAddressType() {
		return addressType;
	}

	public void setAddressType(Integer addressType) {
		this.addressType = addressType;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getCreaterId() {
		return createrId;
	}

	public void setCreaterId(String createrId) {
		this.createrId = createrId;
	}

	public String getCreaterName() {
		return createrName;
	}

	public void setCreaterName(String createrName) {
		this.createrName = createrName;
	}

	public String getPlatformCode() {
		return platformCode;
	}

	public void setPlatformCode(String platformCode) {
		this.platformCode = platformCode;
	}

	public Integer getDeleted() {
		return deleted;
	}

	public void setDeleted(Integer deleted) {
		this.deleted = deleted;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	@Override
	public String toString() {
		return "SysConfig [id=" + id + ", domainId=" + domainId + ", keyName="
				+ keyName + ", value=" + value + ", configType=" + configType
				+ ", addressType=" + addressType + ", describe=" + describe
				+ ", updateTime=" + updateTime + ", createTime=" + createTime
				+ ", createrId=" + createrId + ", createrName=" + createrName
				+ ", platformCode=" + platformCode + ", deleted=" + deleted
				+ "]";
	}
	public String getCustomConfigType() {
		return customConfigType;
	}
	public void setCustomConfigType(String customConfigType) {
		this.customConfigType = customConfigType;
	}
	
	public String getAddressInfo() {
		if (addressInfo != null && !"".equals(addressInfo)) {
			return addressInfo;
		} else {
			if ( value != null ) {
				int start = value.indexOf("//");
				if (start > 0) {
					int end = value.indexOf("/", start + 2);
					if (end > 0) {
						addressInfo = value.substring(start + 2, end);
					}
				}
			}
		}
		
		return addressInfo;
	}

	/**
	 * 设置接口地址，并修改value的值，修改成功返回1，否则返回0
	 * @param addressInfo
	 */
	public int setAddressInfo(String addressInfo) {
		this.addressInfo = addressInfo;
		//比较特殊的配置处理,后面没有用  / 结尾
		String[] keyNameArray={"localCmsFt","localCmsGateway","localCmsPortal"};
		
		if (value != null) {
			int start = value.indexOf("//");
			if (start > 0) {
				int end = value.indexOf("/", start + 2);
				if (end > 0) {
					String preStr = value.substring(0, start + 2);
					String endStr = value.substring(end);
					value =  preStr + addressInfo + endStr;
					return 1;
				}else if(Arrays.asList(keyNameArray).contains(keyName)){
					String preStr = value.substring(0, start + 2);
					value =  preStr + addressInfo;
					return 1;
				}
			}
		}
		
		//value的值没被修改，则返回0
		return 0;
	}

}
